﻿using System;
using System.Collections.Generic;
using System.Text;
using DataService.Core.Common;
using DataService.Core.IO;
using DataService.Office.Excel;
using Mysqlx.Crud;

namespace UnitTest.Excel
{
    public class TestCsv
    {
        /// <summary>
        /// 写数据到 数据流
        /// </summary>
        public void MemoryTest()
        {
            var ms = new MemoryStream();

            var list = new List<Object[]>
            {
                new Object[] { 1234, "数据到流1", true, DateTime.Now },
                new Object[] { 5678, "数据到流2", false, DateTime.Today }
            };
            using (var csv = new CsvFile(ms, true))
            {
                csv.Separator = ',';
                csv.Encoding = Encoding.UTF8;

                csv.WriteLine(new[] { "Code", "Name", "Enable", "CreateTime" });
                csv.WriteAll(list);
            }
            var txt = ms.ToArray().ToStr();
            Console.WriteLine(txt);
            Console.WriteLine("此时内存使用量为:{2}KB,该MemoryStream的已经使用的容量为{0}byte,默认容量为{1}byte",Math.Round((double)ms.Length, 4), ms.Capacity, GC.GetTotalMemory(false) / 1024);
        }
        /// <summary>
        /// 写数据到文件
        /// </summary>
        public void FileTest()
        {
            var file = "data/test.csv";

            var list = new List<Object[]>
            {
                new Object[] { 1234, "数据到文件1", true, DateTime.Now },
                new Object[] { 5678, "数据到文件2", false, DateTime.Today }
            };
            using (var csv = new CsvFile(file, true))
            {
                csv.Separator = ',';
                csv.Encoding = Encoding.UTF8;

                csv.WriteLine(new[] { "Code", "Name", "Enable", "CreateTime" });
                csv.WriteAll(list);
            }

        }

        public void ReadTest()
        {
            var file = @"D:\资料相关\框架代码\DataService\UnitTest\bin\Debug\netcoreapp3.1\data\test.csv";
            using var csv = new CsvFile(file);
            var headers = csv.ReadLine();
            StringBuilder sb = new StringBuilder();
            foreach (string itemHead in headers)
            {
                sb.Append(itemHead).Append(",");
            }
            sb.Length--;
            Console.WriteLine(sb.ToString());
            var all = csv.ReadAll();
            foreach (string[] item in all)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (string itemRow in item)
                {
                    sb1.Append(itemRow).Append(",");
                }
                sb1.Length--;
                Console.WriteLine(sb1.ToString());
            }
        }
    }
}
